package com.sudoku;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.sudoku.device.Device;
import com.sudoku.device.DeviceAndroid;
import com.sudoku.device.DeviceIOS;
import com.sudoku.device.DevicePC;
import com.sudoku.observer.Event;
import com.sudoku.observer.Subject;
import com.sudoku.screen.GameScreen;
import com.sudoku.screen.LoadingAssetsScreen;
import com.sudoku.screen.MenuScreen;
import com.sudoku.screen.OptionsScreen;
import com.sudoku.screen.PuzzleLoadingScreen;
import com.sudoku.services.GameServiceInterface;
import com.sudoku.sound.AudioManager;
import com.sudoku.sound.ConsoleAudio;
import com.sudoku.utility.Constants;
import com.sudoku.utility.Locator;

public class Sudoku extends Game implements Constants {
	private MenuScreen menu;
	private GameScreen gameScreen;
	private AudioManager audioManager;
	private OrthographicCamera camera;
	private Subject subject;
	private FitViewport viewport;
	private GameServiceInterface ads;
	private PuzzleLoadingScreen puzzleLoadingScreen;
	private OptionsScreen optionsScreen;

	public Sudoku() {
	}

	public Sudoku(GameServiceInterface ads) {
		this.ads = ads;
	}

	@Override
	public void create() {
		Device device;
		switch (Gdx.app.getType()) {
		case Android:
			device = new DeviceAndroid(ads);
			break;
		case Desktop:
			device = new DevicePC();
			break;
		case iOS:
			device = new DeviceIOS(ads);
			break;
		default:
			device = new DevicePC();
			break;

		}

		camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
		camera.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		camera.update();

		viewport = new FitViewport(ACTUAL_VIRTUAL_WIDTH, ACTUAL_VIRTUAL_HEIGHT, camera);
		viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		subject = new Subject();

		Locator.init(this, device, new ConsoleAudio(), camera, viewport, subject);

		setScreen(new LoadingAssetsScreen());
	}

	public void init() {
		audioManager = new AudioManager(Locator.getAudio(), Locator.isMute(), subject);
		gameScreen = new GameScreen(camera, audioManager, subject, this);
		puzzleLoadingScreen = new PuzzleLoadingScreen(subject);
		Locator.readSave();
		menu = new MenuScreen(this, audioManager, subject, camera);
		optionsScreen = new OptionsScreen(this, menu, audioManager, subject);
		setScreen(menu);
	}

	public GameScreen getGameScreen() {
		return gameScreen;
	}

	public MenuScreen getMenuScreen() {
		return menu;
	}

	@Override
	public void render() {
		super.render();
	}

	@Override
	public void dispose() {
		super.dispose();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}

	@Override
	public void pause() {
		super.pause();
	}

	@Override
	public void resume() {
		super.resume();
	}

	public PuzzleLoadingScreen getPuzzleLoadingScreen() {
		return puzzleLoadingScreen;
	}

	public OptionsScreen getOptionsScreen() {
		return optionsScreen;
	}

	public GameServiceInterface getAds() {
		return ads;
	}

	public void adClosed() {
		subject.notify(Event.EVENT_ADS_DONE);

	}
}
